package cmd

import (
	"io"
	"k8s-gpu-collector/pkg/config"
	"k8s-gpu-collector/pkg/k8s"
	"k8s-gpu-collector/pkg/rpcserver"
	"os"

	"github.com/gin-gonic/gin"
	"github.com/spf13/cobra"
)

func init() {
	// gin global config
	gin.SetMode(gin.ReleaseMode)
	// disable gin's native logger
	gin.DefaultWriter = io.Discard
}

var rootCmd = &cobra.Command{
	Use:   "k8s-gpu-collector",
	Short: "gpu资源指标收集",
	Run:   run,
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
	err := rootCmd.Execute()
	if err != nil {
		os.Exit(1)
	}
}

func run(cmd *cobra.Command, args []string) {
	cfg, err := config.InitGlobalConfig()
	if err != nil {
		panic(err)
	}

	//srv, err := server.NewServer(cfg)
	//if err != nil {
	//	panic(err)
	//}
	//_ = srv.Run()
	cs, err := k8s.NewClientSet(cfg.InCluster, cfg.KubeconfigFile)
	if err != nil {
		panic(err)
	}
	s := rpcserver.NewRpcServer(cs, cfg.Port)
	_ = rpcserver.RunServer(s)
}
